﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Net.Http;
using System.Net.Http.Headers;
using SWEN_HMS.Classes;

namespace SWEN_HMS.StaffHousekeepingModule
{
    public partial class AddStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string staffName = tbxstaffName.Text;
            string password = tbxPW.Text;
            string dob = tbxDate.Text;
            string email = tbxEmail.Text;
            int contact = 0;
            string address = tbxAddress.Text;
            string bankType = ddlBank.SelectedValue;
            int accountNo = 0;
            string position = ddlPosition.SelectedValue;

            string accessLevel = tbxAccess.Text;

            if (tbxContact.Text.Length != 8)
            {
                tbxContact.Text = "";
                tbxPW.Text = "";
                tbxRPW.Text = "";
                tbxEmail.Text = "";
                System.Windows.Forms.MessageBox.Show("Invalid Mobile Number");

            }
            else
            {
                contact = Convert.ToInt32(tbxContact.Text);
                if (tbxAccountNO.Text.Length != 9)
                {
                    tbxAccountNO.Text = "";
                    tbxPW.Text = "";
                    tbxRPW.Text = "";
                    tbxEmail.Text = "";
                    System.Windows.Forms.MessageBox.Show("Invalid account number");

                }
                else
                {
                    accountNo = Convert.ToInt32(tbxAccountNO.Text);

                }
            }
            if (tbxEmail.Text != "")
            {
                staff s = new staff();
                EncodingAndDecodingcs encode = new EncodingAndDecodingcs();
                s.staffName = staffName;
                s.password = encode.Encode(password);
                s.email = email;
                s.dob = dob;
                s.contactNo = contact;
                s.address = address;
                s.bankType = bankType;
                s.accountNo = accountNo;
                s.position = position;
                s.accessLevel = accessLevel;

                int row = userDB.registerStaff(s);

                if (row > 0)
                {

                    if ((tbxPW.Text != ""))
                    {
                        staff staff = userDB.getDetailByStaffName((string)staffName);
                        if (s != null)
                        {
                            int id = staff.staffId; ;
                            MailMessage mail = new MailMessage();
                            mail.From = new MailAddress("swenhms@gmail.com");
                            mail.To.Add(s.email);
                            mail.Subject = "Delonix Regia Account Access";
                            mail.Body = "Dear " + staffName + "," + Environment.NewLine + Environment.NewLine + "Here is your login pass and your password to access to the Delonix Regia HMS" + Environment.NewLine + Environment.NewLine + "StaffID:" + id + Environment.NewLine +
                                "Password:" + encode.Decode(s.password) + Environment.NewLine + Environment.NewLine + "This is a generated E-mail. Please do not reply" + Environment.NewLine + Environment.NewLine + "Best Regards" + Environment.NewLine + " Delonix Regia Team";

                            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                            smtp.Credentials = new NetworkCredential("swenhms@gmail.com", "HMS12345");
                            smtp.Port = 587;
                            smtp.EnableSsl = true;
                            smtp.Send(mail);

                        }

                        System.Windows.Forms.MessageBox.Show("Add staff Successful! Account Detail been sent to the staff Email.");
                        Server.Transfer("~/StaffHousekeepingModule/ViewStaff.aspx");

                    }

                    else
                    {
                        System.Windows.Forms.MessageBox.Show("Add staff Successful!");
                        Server.Transfer("~/StaffHousekeepingModule/ViewStaff.aspx");
                    }
                }
                else
                {
                    lblerrormessage.Text = "Adding staff fail!";
                }






            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Add staff UnSuccessful!");
            }


            }

        protected void ddlPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlPosition.SelectedValue == "General Maintenance" || ddlPosition.SelectedValue == "Security" || ddlPosition.SelectedValue == "Room Maintenance"|| ddlPosition.SelectedValue=="Estate Maintenance")
            {
                tbxAccess.Text = "No Access";
                tbxPW.ReadOnly = true;
                tbxRPW.ReadOnly = true;
               
            }
            else if (ddlPosition.SelectedValue == "Reception")
            {
                tbxAccess.Text = "Reception";
                tbxPW.ReadOnly = false;
                tbxRPW.ReadOnly = false;
               
            }
            else if(ddlPosition.SelectedValue == "Manager")
            {
                tbxAccess.Text = "Management";
                tbxPW.ReadOnly = false;
                tbxRPW.ReadOnly = false;
               
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/ViewStaff.aspx");
        }


        }
    }